package in.muton.edushare.grading.panels.prevgrades.old;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.List;

import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.BatchDAO;
import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.CurriculumDAO;
import com.edushare.common.dao.EnrolmentsDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.AcademicYearModel;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.Users;

public class ElectiveModel implements IChoiceRenderer<IModel<Courses>>,
		IModel<IModel<Courses>> {

	private IModel<Batches> batchModel;
	private int groupId;
	private CourseGradeField courseGradeField;
	private int currSem;
	private IModel<Courses> selectedCourse = null;

	public ElectiveModel(IModel<Users> userModel, IModel<Batches> batch,
			int groupId, CourseGradeField field, int semNum) {
		this.batchModel = batch;
		this.groupId = groupId;
		this.courseGradeField = field;
		this.currSem = semNum;
		this.selectedCourse = getActiveCourse(userModel, batch, semNum, groupId);
	}

	public static IModel<Courses> getActiveCourse(IModel<Users> user,
			IModel<Batches> b, int semNum, int groupId) {
		CourseInstances c = getActiveInst(user, b, semNum, groupId).getObject();
		return new CayenneModel<Courses>(c.getCourse());
	}

	public static IModel<CourseInstances> getActiveInst(IModel<Users> user,
			IModel<Batches> b, int semNum, int groupId) {
		IModel<Courses> course = getActiveCourse(user, b, semNum, groupId);
		CourseInstancesDAO cDAO = new CourseInstancesDAO(getORM());
		BatchDAO bDAO = new BatchDAO(getORM());
		int year = bDAO.getCalendarYear(b.getObject(), semNum);
		AcademicYearModel sem = bDAO.getAcadYearModel(b.getObject(), semNum);
		CourseInstances cInst = cDAO.getInstanceByYearAndSem(
				course.getObject(), year, sem);
		Enrolments e = new EnrolmentsDAO(getORM()).getEnrolment(cInst, user
				.getObject());
		if (e.getElectiveGroupId() == groupId) {
			return new CayenneModel<CourseInstances>(cInst);
		} else
			return null;
	}

	public List<IModel<Courses>> getOptionsList() {
		CurriculumDAO cDAO = new CurriculumDAO(getORM());
		List<Courses> coursesList = cDAO.getElectiveCourses(batchModel
				.getObject().getCurricula(), currSem, groupId);
		return new CayenneList<Courses>(coursesList).getObject();
	}

	public Object getDisplayValue(IModel<Courses> arg0) {
		return arg0.getObject().getCourseName();
	}

	public String getIdValue(IModel<Courses> arg0, int arg1) {
		return arg0.getObject().getCourseId().toString();
	}

	public IModel<Courses> getObject() {
		return selectedCourse;
	}

	public void setObject(IModel<Courses> arg0) {
		selectedCourse = arg0;
		BatchDAO bDAO = new BatchDAO(getORM());
		CourseInstancesDAO cDAO = new CourseInstancesDAO(getORM());
		int year = bDAO.getCalendarYear(batchModel.getObject(), currSem);
		AcademicYearModel sem = bDAO.getAcadYearModel(batchModel.getObject(),
				currSem);
		CourseInstances cInst = cDAO.getInstanceByYearAndSem(selectedCourse
				.getObject(), year, sem);
		if (cInst == null) {
			cInst = cDAO.createInstanceByYearAndSem(selectedCourse.getObject(),
					year, sem);
		}
		courseGradeField
				.setCourseModel(new CayenneModel<CourseInstances>(cInst));
	}

	private static ORMInterface getORM() {
		return EduShareSession.get().getSessionORMInterface();
	}

	public void detach() {
	}

	private static final long serialVersionUID = -7844400362770342288L;
}
